package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.OnlineUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.OnlineUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.OnlineUploadDao;
import io.renren.modules.basedata.entity.tbl_online;
import io.renren.modules.basedata.service.OnlineUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;


@Service
public class OnlineUploadServiceImpl implements OnlineUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private OnlineUploadDao onlineUploadDao;

    @Override
    public String onlineUpload() throws Exception {
        log.info("开始获取线上服务渠道信息！");
        int operResult = 0;
        List<tbl_online> list = null;
        tbl_online online = null;
        list = getOnlineList();//获取线上服务渠道信息
        if(list!=null&&list.size()>0){
            //一条一条上传数据
            for(int i=0;i<list.size();i++){
                online = list.get(i);
                OnlineUploadRequest request = new OnlineUploadRequest();
                request.setId(online.getId());
                request.setName(online.getName());
                request.setServiceItems(online.getServiceItems());
                request.setStartTime(DateUtil.formatDate(online.getStartTime()));
                request.setEndTime(DateUtil.formatDate(online.getEndTime()));
                request.setOperation(online.getFlag());

                String fileName = ETCCommHelper.getFileName(request,"65");
                //生成就json文件
//					CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);

                try{
                    OnlineUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, OnlineUploadResponse.class);
                    log.info("上传线上服务器成功，服务器返回："+response.toJson());
                    if(response!=null&&response.getInfo().equals("成功！")){//表示上传成功

                        //更新线上服务渠道信息表中的上传标志
                        if(onlineUploadDao.updateOnlineUploadflag(online.getId())==1){
                            log.info("线上服务渠道信息表上传标志更新成功！");
                        }else{
                            log.info("线上服务渠道信息表上传标志更新失败！");
                        }
                    }else{
                        log.info("上传线上服务渠道信息失败!失败原因："+response.getInfo());

                        onlineUploadDao.updateOnlineUploadflagError(online.getId());

                    }
                }catch(IOException e){
                    log.error("上传线上服务渠道信息失败！错误信息：",e);

                    onlineUploadDao.updateOnlineUploadflagError(online.getId());

                    e.printStackTrace();
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传线上服务渠道信息失败！错误信息：",apie);

                    onlineUploadDao.updateOnlineUploadflagError(online.getId());

                    apie.printStackTrace();
                    continue;
                }
            }
            return "上传线上服务渠道信息结束！";
        }else{
            return "无线上服务渠道信息，上传结束...";
        }
    }

    //获取线上服务渠道信息
    @SuppressWarnings("unchecked")
    public List<tbl_online> getOnlineList() throws IOException {
        List<tbl_online> list;
        try{
            list = onlineUploadDao.getOnlineList();
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取线上服务渠道信息失败！失败原因：",e);
            throw new IOException("获取线上服务渠道信息失败:"+e.getMessage());
        }
        return list;
    }


}
